import numpy as np


def join_line_array(arr):
    arr_copied = arr.copy()
    arr_flattened = arr.flatten()

    arr_unique = np.unique(arr_flattened)
    arr_unique_original = arr_unique.copy()

    def fun(num): # to return the count which a single number repeats
        return len(arr_flattened[arr_flattened == num])

    vfunc = np.vectorize(fun)
    result2 = vfunc(arr_unique)

    index_and_count = np.array([arr_unique, result2])
    index_and_count_t = index_and_count.T

    index_and_count_ends_0 = index_and_count_t[np.where(index_and_count_t[:, 1] != 2)]
    vertex_index_ends = index_and_count_ends_0[:, 0]
    vertex_index_ends_original = vertex_index_ends.copy()

    current_vertex_index = -1
    if len(vertex_index_ends) == 0: current_vertex_index = arr_unique[0]
    else: current_vertex_index = vertex_index_ends[0] #当前的端点节点编码

    test_count = 0
    all_paths = []
    single_path = [current_vertex_index]

    while len(arr_copied) != 0:
        if single_path[0] == single_path[-1] and len(single_path) >= 2:
            all_paths.append(single_path); single_path = [current_vertex_index]

        if np.isin(current_vertex_index, vertex_index_ends):
            if len(single_path) >= 2:
                all_paths.append(single_path); single_path = [current_vertex_index]
        lines_across_current_vertex_index = arr_copied[np.any(arr_copied == current_vertex_index, axis=1)]
        while len(lines_across_current_vertex_index) == 0 and len(arr_copied) != 0:
            vertex_index_ends = vertex_index_ends[vertex_index_ends != current_vertex_index]
            arr_unique = arr_unique[arr_unique != current_vertex_index]
            current_vertex_index = -1

            if len(vertex_index_ends) != 0: current_vertex_index = vertex_index_ends[0]
            else: current_vertex_index = arr_unique[0]

            single_path = [current_vertex_index]
            lines_across_current_vertex_index = arr_copied[np.any(arr_copied == current_vertex_index, axis=1)]

        if len(arr_copied) != 0:
            arr_unique = arr_unique[arr_unique != current_vertex_index]
            current_line = arr_copied[np.any(arr_copied == current_vertex_index, axis=1)][0]
            current_vertex_index = current_line[current_line != current_vertex_index][0]

            single_path.append(current_vertex_index)
            arr_copied = arr_copied[(arr_copied != current_line).any(axis=1)]

    if len(single_path) >= 2:
        all_paths.append(single_path); single_path = [current_vertex_index]


    return all_paths